{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 接入AI的API调用，实现简单对话",
   "id": "fe695dc1a9455f67"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-28T16:51:03.157352Z",
     "start_time": "2025-07-28T16:50:29.103598Z"
    }
   },
   "source": [
    "import os\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "# 加载环境变量\n",
    "load_dotenv()\n",
    "\n",
    "# 初始化模型\n",
    "# llm = ChatOpenAI(\n",
    "#     api_key=os.getenv(\"DASHSCOPE_KEY\"),\n",
    "#     base_url=os.getenv(\"DASHSCOPE_OPENAI_URL\"),\n",
    "#     model=\"qwen-plus-1220\"\n",
    "# )\n",
    "llm = ChatOpenAI(\n",
    "    api_key=os.getenv(\"ZHIPU_KEY\"),\n",
    "    base_url=os.getenv(\"ZHIPU_OPENAI_URL\"),\n",
    "    model=os.getenv(\"ZHIPU_CHAT_MODEL\")\n",
    ")\n",
    "\n",
    "# 正确使用 astream 的方式\n",
    "# async def get_model_response():\n",
    "#     response_content = \"\"\n",
    "#     async for chunk in llm.astream(\"你是什么模型，具体的模型名，比如qwen3\"):\n",
    "#         if chunk.content:\n",
    "#             response_content += chunk.content\n",
    "#             print(chunk.content, end=\"\", flush=True)  # 实时打印响应\n",
    "#     return response_content\n",
    "\n",
    "\n",
    "# 调用函数\n",
    "response = await llm.ainvoke(\"给我制定一个健身计划,文档需要美化，多一些图标\")\n",
    "print(f\"\\n完整响应: {response.content}\")\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "完整响应: # 🏋️‍♂️ 8周科学健身计划 · 塑形增肌/减脂双向版\n",
      "\n",
      "\n",
      "## 📋 计划概述  \n",
      "### 🎯 目标设定  \n",
      "- **增肌方向**：提升肌肉维度与力量（每周增重0.25-0.5kg）  \n",
      "- **减脂方向**：降低体脂率，塑造线条（每周减重0.5-1kg）  \n",
      "- **周期**：8周（可循环进阶）  \n",
      "- **适合人群**：健身新手/初级进阶者（无需器械基础可选徒手版）  \n",
      "\n",
      "\n",
      "### 📅 训练频率与分化  \n",
      "| 周期   | 训练安排          | 每次时长   | 重点目标               |  \n",
      "|--------|-------------------|------------|------------------------|  \n",
      "| 第1-4周 | 全身训练（每周3次）| 45-60分钟  | 适应动作模式，激活肌群 |  \n",
      "| 第5-8周 | 上下肢分化（每周4次）| 60-75分钟  | 针对性强化，提升效率   |  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## 🏋️‍♂️ 第1-4周：全身激活计划（每周3次，隔天训练）  \n",
      "### 训练日示例（周一/三/五）  \n",
      "**训练前**：热身10分钟（动态拉伸+开合跳）  \n",
      "**训练后**：拉伸10分钟（静态放松）  \n",
      "\n",
      "\n",
      "#### 📝 动作清单（每个动作3组，组间休息60-90秒）  \n",
      "| 部位   | 动作名称         | 组数×次数       | 器械选择       | 图标       |  \n",
      "|--------|------------------|-----------------|----------------|------------|  \n",
      "| 胸部   | 标准俯卧撑       | 3×12-15         | 徒手           | 💪         |  \n",
      "| 背部   | 弹力带划船       | 3×15-20         | 弹力带         | 🎣         |  \n",
      "| 腿部   | 自重深蹲         | 3×15-20         | 徒手           | 🦵         |  \n",
      "| 肩部   | 哑铃推举         | 3×12-15（每侧） | 哑铃（5-10kg） | 🏋️         |  \n",
      "| 核心   | 平板支撑         | 3×30-60秒       | 徒手           | 🧘‍♀️        |  \n",
      "| 臀部   | 臀桥             | 3×20-25         | 徒手/哑铃      | 🍑         |  \n",
      "\n",
      "\n",
      "### 💡 新手提示  \n",
      "- 动作优先标准，而非重量：俯卧撑膝盖着地、深蹲膝盖不超过脚尖  \n",
      "- 每周可增加1-2次/组，逐步提升负荷  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## 🏋️‍♀️ 第5-8周：上下肢分化计划（每周4次，练1休1）  \n",
      "### 📅 周期安排  \n",
      "- **上肢日**（周一/四）：胸+肩+背+手臂  \n",
      "- **下肢日**（周二/五）：腿+臀+核心  \n",
      "- **休息日**（周三/六/日）：有氧或动态拉伸  \n",
      "\n",
      "\n",
      "#### 🔥 上肢日训练计划  \n",
      "**热身**：肩绕环+手臂画圈（各15次）+弹力带肩外旋（3×12）  \n",
      "\n",
      "| 部位   | 动作名称         | 组数×次数       | 器械选择       | 图标       |  \n",
      "|--------|------------------|-----------------|----------------|------------|  \n",
      "| 胸部   | 杠铃平板卧推     | 4×8-12          | 杠铃（60%1RM） | 🏋️‍♂️       |  \n",
      "| 背部   | 引体向上（辅助） | 4×10-15         | 引体向上架     | 🪜         |  \n",
      "| 肩部   | 哑铃侧平举       | 3×15-20（每侧） | 哑铃（3-5kg）  | 🌊         |  \n",
      "| 手臂   | 绳索下压         | 3×12-15         | 高位滑轮机     | 🎯         |  \n",
      "| 核心   | 俄罗斯转体       | 3×20-30（每侧） | 哑铃（2-3kg）  | 🔄         |  \n",
      "\n",
      "\n",
      "#### 🦵 下肢日训练计划  \n",
      "**热身**：高抬腿+后踢腿（各30秒）+徒手深蹲（15次）  \n",
      "\n",
      "| 部位   | 动作名称         | 组数×次数       | 器械选择       | 图标       |  \n",
      "|--------|------------------|-----------------|----------------|------------|  \n",
      "| 腿部   | 杠铃深蹲         | 4×8-10          | 杠铃（70%1RM） | 🏋️‍♀️       |  \n",
      "| 臀部   | 罗马尼亚硬拉     | 3×12-15         | 哑铃（10-15kg）| 🦴         |  \n",
      "| 小腿   | 站姿提踵         | 4×20-25         | 哑铃/器械      | 🦶         |  \n",
      "| 核心   | 仰卧举腿         | 3×15-20         | 徒手           | 🚀         |  \n",
      "| 平衡   | 单腿硬拉         | 3×10-12（每侧） | 哑铃（5-8kg）  | ⚖️         |  \n",
      "\n",
      "\n",
      "### 💪 进阶原则  \n",
      "- **渐进超负荷**：每周增加2.5-5kg重量，或增加1-2次/组  \n",
      "- **动作升级**：俯卧撑→窄距俯卧撑；深蹲→保加利亚分腿蹲  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## 🍎 饮食建议（同步关键！）  \n",
      "### 📊 营养素比例（根据目标调整）  \n",
      "| 目标   | 蛋白质 | 碳水化合物 | 脂肪 | 每日热量参考 |  \n",
      "|--------|--------|------------|------|--------------|  \n",
      "| 增肌   | 30%    | 50%        | 20%  | 体重×（35-40）kcal |  \n",
      "| 减脂   | 35%    | 35%        | 30%  | 体重×（25-30）kcal |  \n",
      "\n",
      "\n",
      "### 🍽️ 一日餐次示例（增肌版）  \n",
      "- **早餐**（7:00）：燕麦牛奶粥+鸡蛋2个+香蕉1根 🥣🍳🍌  \n",
      "- **加餐**（10:00）：蛋白粉1勺+全麦面包2片 🥤🍞  \n",
      "- **午餐**（12:30）：糙米饭1碗+鸡胸肉150g+西兰花200g 🍚🍗🥦  \n",
      "- **训练后**（17:00）：蛋白粉1勺+酸奶1杯 🥤🥛  \n",
      "- **晚餐**（18:30）：清蒸鱼200g+红薯1个+生菜沙拉 🐟🍠🥬  \n",
      "\n",
      "\n",
      "### 🥗 减脂版调整  \n",
      "- 替换碳水为粗粮（玉米、紫薯）  \n",
      "- 减少烹饪用油，优先蒸煮  \n",
      "- 餐前喝300ml水，增加饱腹感 💧  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## 😴 休息与恢复  \n",
      "### 🌙 睡眠管理  \n",
      "- 每晚7-8小时睡眠（22:00-23:00入睡最佳）  \n",
      "- 睡前1小时远离电子设备，可泡脚或听轻音乐 🛁🎵  \n",
      "\n",
      "\n",
      "### 🧘 拉伸与放松  \n",
      "- 训练后必做：泡沫轴放松（大腿、背部、臀部各2分钟）  \n",
      "- 每周1次瑜伽或普拉提，提升柔韧性 🧘‍♂️🌸  \n",
      "\n",
      "\n",
      "### ⏸️ 休息日安排  \n",
      "- 可进行低强度有氧（快走、游泳30分钟）  \n",
      "- 避免连续2天高强度训练，肌肉修复需要48小时 🚶‍♀️🏊‍♀️  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## ⚠️ 注意事项  \n",
      "1. **热身不偷懒**：动态拉伸激活关节，预防拉伤 🔥  \n",
      "2. **动作＞重量**：错误动作伤关节，可对着镜子或录视频纠正 🪞  \n",
      "3. **补水要充足**：每天饮水2-3L，训练中每15分钟喝150-200ml 💧  \n",
      "4. **记录进度**：每周称体重、量围度，拍照对比，保持动力 📸  \n",
      "\n",
      "\n",
      "---\n",
      "\n",
      "## 🎯 计划调整与坚持  \n",
      "- **第4周/第8周**：评估进度，若未达目标可增加训练量或调整饮食  \n",
      "- **平台期突破**：尝试改变动作顺序（如先做深蹲再做硬拉）或增加超级组 💥  \n",
      "- **奖励机制**：完成4周奖励自己新运动装备/按摩，保持积极性 🎁  \n",
      "\n",
      "\n",
      "**坚持8周，遇见更自律的自己！** 🌟  \n",
      "（可打印贴在墙上，每日打卡记录）\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:20.679579Z",
     "start_time": "2025-07-25T17:11:19.769881Z"
    }
   },
   "cell_type": "code",
   "source": "llm.invoke(\"你是什么模型\").content",
   "id": "32847537662ef4a4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我是来自阿里云的超大规模语言模型，我叫通义千问。'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 包含上下文的对话",
   "id": "eb9548409879a25d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:20.869937Z",
     "start_time": "2025-07-25T17:11:20.852158Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from typing import List\n",
    "from pydantic import BaseModel, Field\n",
    "from langchain_core.messages import BaseMessage, HumanMessage\n",
    "\n",
    "\n",
    "# 全局对话上下文\n",
    "class SimpleState(BaseModel):\n",
    "    query: str  #用户请求\n",
    "    chat_history: List[BaseMessage] = Field(default_factory=list)  #对话历史\n",
    "\n",
    "    def add_message(self, message: BaseMessage):\n",
    "        \"\"\"添加历史对话到上下文\"\"\"\n",
    "        self.chat_history.append(message)\n",
    "\n",
    "    def clear_history(self):\n",
    "        self.chat_history.clear()\n",
    "\n",
    "\n",
    "state = SimpleState(query=\"\")\n",
    "\n",
    "\n",
    "async def chat(query: str) -> str:\n",
    "    \"\"\"简单包含历史的对话\"\"\"\n",
    "    #先添加用户对话到历史消息\n",
    "    state.add_message(HumanMessage(content=query))\n",
    "    #将整个消息列表喂给AI\n",
    "    response = await llm.ainvoke(state.chat_history)\n",
    "    #保存AI的响应\n",
    "    state.add_message(response)\n",
    "    return response.content\n",
    "\n",
    "\n",
    "def clear_chat():\n",
    "    \"\"\"清空聊天历史\"\"\"\n",
    "    state.clear_history()\n",
    "    print(\"聊天历史已清空\")\n",
    "\n",
    "\n",
    "# 查看当前历史记录\n",
    "def show_history():\n",
    "    \"\"\"显示当前聊天历史\"\"\"\n",
    "    print(f\"历史记录数量: {len(state.chat_history)}\")\n",
    "\n",
    "    if not state.chat_history:\n",
    "        print(\"暂无对话历史\")\n",
    "        return\n",
    "\n",
    "    for i, message in enumerate(state.chat_history):\n",
    "        role = \"用户\" if isinstance(message, HumanMessage) else \"AI\"\n",
    "        print(f\"{i + 1}. {role}: {message.content}\")"
   ],
   "id": "eeca1a51b8c28b65",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:22.569877Z",
     "start_time": "2025-07-25T17:11:21.032662Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一轮对话\n",
    "response = await chat(\"你好，我叫小明\")\n",
    "print(f\"AI: {response}\")"
   ],
   "id": "e72e3461ac067833",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AI: 你好，小明！很高兴认识你。有什么我可以帮助你的吗？\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:23.530070Z",
     "start_time": "2025-07-25T17:11:22.732189Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第二轮对话（AI会记住之前的对话）\n",
    "response = await chat(\"我刚才说了我叫什么名字吗？\")\n",
    "print(f\"AI: {response}\")"
   ],
   "id": "c22bbc2a87130694",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AI: 你说你叫小明。需要我帮你确认或记录什么信息吗？\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:23.658958Z",
     "start_time": "2025-07-25T17:11:23.651411Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看对话历史\n",
    "show_history()"
   ],
   "id": "d77edfdac31c6f47",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "历史记录数量: 4\n",
      "1. 用户: 你好，我叫小明\n",
      "2. AI: 你好，小明！很高兴认识你。有什么我可以帮助你的吗？\n",
      "3. 用户: 我刚才说了我叫什么名字吗？\n",
      "4. AI: 你说你叫小明。需要我帮你确认或记录什么信息吗？\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:23.838489Z",
     "start_time": "2025-07-25T17:11:23.831367Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 清空历史\n",
    "clear_chat()"
   ],
   "id": "2fb1830f63fc8f56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "聊天历史已清空\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:23.952930Z",
     "start_time": "2025-07-25T17:11:23.945586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看对话历史\n",
    "show_history()"
   ],
   "id": "f2a05312ebd86ebe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "历史记录数量: 0\n",
      "暂无对话历史\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 调用简单工具",
   "id": "8c9962179915b7e7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:12:00.210254Z",
     "start_time": "2025-07-25T17:12:00.196088Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from datetime import datetime\n",
    "import random\n",
    "from langchain_core.tools import StructuredTool\n",
    "from typing import Optional\n",
    "\n",
    "\n",
    "def get_current_date() -> str:\n",
    "    \"\"\"\n",
    "    获取当前日期\n",
    "    Returns:\n",
    "        str: 格式化的当前日期字符串\n",
    "    \"\"\"\n",
    "    today_time = datetime.now()\n",
    "    return today_time.strftime(\"%Y年%m月%d日 %A\")\n",
    "\n",
    "\n",
    "def get_current_weather(location: str = \"北京\") -> dict:\n",
    "    \"\"\"\n",
    "    获取指定位置的当前天气信息（模拟数据）\n",
    "    Args:\n",
    "        location (str): 位置名称，默认为\"北京\"\n",
    "    Returns:\n",
    "        dict: 包含天气信息的字典\n",
    "    \"\"\"\n",
    "    # 模拟天气数据\n",
    "    weather_conditions = [\"晴天\", \"多云\", \"阴天\", \"小雨\", \"大雨\", \"雪\", \"雾\"]\n",
    "    wind_directions = [\"北风\", \"南风\", \"东风\", \"西风\", \"东北风\", \"西北风\", \"东南风\", \"西南风\"]\n",
    "\n",
    "    # 随机生成天气数据\n",
    "    temperature = random.randint(-10, 35)  # 随机温度\n",
    "    condition = random.choice(weather_conditions)\n",
    "    humidity = random.randint(30, 90)  # 随机湿度\n",
    "    wind_speed = random.randint(0, 20)  # 随机风速\n",
    "    wind_direction = random.choice(wind_directions)\n",
    "\n",
    "    return {\n",
    "        \"location\": location,\n",
    "        \"temperature\": temperature,\n",
    "        \"condition\": condition,\n",
    "        \"humidity\": humidity,\n",
    "        \"wind_speed\": wind_speed,\n",
    "        \"wind_direction\": wind_direction,\n",
    "        \"description\": f\"{location}当前天气为{condition}，温度{temperature}°C，湿度{humidity}%，{wind_direction}{wind_speed}km/h\"\n",
    "    }\n",
    "\n",
    "\n",
    "# 定义输入模型\n",
    "class DateInput(BaseModel):\n",
    "    pass\n",
    "\n",
    "\n",
    "class WeatherInput(BaseModel):\n",
    "    location: Optional[str] = Field(default=\"北京\", description=\"城市名称\")\n",
    "\n",
    "\n",
    "# 使用 StructuredTool 创建工具\n",
    "date_tool = StructuredTool.from_function(\n",
    "    func=get_current_date,\n",
    "    name=\"get_current_date\",\n",
    "    description=\"获取当前日期信息\",\n",
    "    args_schema=DateInput\n",
    ")\n",
    "\n",
    "weather_tool = StructuredTool.from_function(\n",
    "    func=get_current_weather,\n",
    "    name=\"get_current_weather\",\n",
    "    description=\"获取指定位置的天气信息\",\n",
    "    args_schema=WeatherInput\n",
    ")\n",
    "\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain.agents import initialize_agent, AgentType\n",
    "\n",
    "tools = [date_tool, weather_tool]\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"You are a helpful assistant.\"),\n",
    "    (\"human\", \"{user_input}\")\n",
    "])\n",
    "parser = StrOutputParser()\n",
    "\n",
    "agent = initialize_agent(\n",
    "    tools=[date_tool, weather_tool],\n",
    "    llm=llm,\n",
    "    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "\n",
    "async def chat_with_tools(query: str):\n",
    "    response_tools = await agent.ainvoke(query)\n",
    "    state.add_message(response)\n",
    "    return response_tools"
   ],
   "id": "88d08863795131e8",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:27.578419Z",
     "start_time": "2025-07-25T17:11:27.570891Z"
    }
   },
   "cell_type": "code",
   "source": "get_current_date()",
   "id": "a931bd1adf673435",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2025年07月26日 Saturday'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:27.833200Z",
     "start_time": "2025-07-25T17:11:27.821270Z"
    }
   },
   "cell_type": "code",
   "source": "get_current_weather(\"上海\")",
   "id": "14d72738f17a95d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'location': '上海',\n",
       " 'temperature': 20,\n",
       " 'condition': '多云',\n",
       " 'humidity': 54,\n",
       " 'wind_speed': 5,\n",
       " 'wind_direction': '南风',\n",
       " 'description': '上海当前天气为多云，温度20°C，湿度54%，南风5km/h'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:27.949722Z",
     "start_time": "2025-07-25T17:11:27.942477Z"
    }
   },
   "cell_type": "code",
   "source": "clear_chat()",
   "id": "3bd52428e780d3d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "聊天历史已清空\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:30.692077Z",
     "start_time": "2025-07-25T17:11:28.055316Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 正确的调用方式\n",
    "response = await chat_with_tools(\"上海今天的天气怎么样？\")\n",
    "print(response)"
   ],
   "id": "f61aa21b21490d03",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
      "\u001B[32;1m\u001B[1;3mAction:\n",
      "```\n",
      "{\n",
      "  \"action\": \"get_current_weather\",\n",
      "  \"action_input\": {\n",
      "    \"location\": \"上海\"\n",
      "  }\n",
      "}\n",
      "``` \n",
      "\u001B[0m\n",
      "Observation: \u001B[33;1m\u001B[1;3m{'location': '上海', 'temperature': -3, 'condition': '雪', 'humidity': 38, 'wind_speed': 10, 'wind_direction': '东南风', 'description': '上海当前天气为雪，温度-3°C，湿度38%，东南风10km/h'}\u001B[0m\n",
      "Thought:\u001B[32;1m\u001B[1;3mI know what to respond\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"上海今天的天气是雪，温度为-3°C，湿度38%，东南风10km/h。\"\n",
      "}\n",
      "```\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "{'input': '上海今天的天气怎么样？', 'output': '上海今天的天气是雪，温度为-3°C，湿度38%，东南风10km/h。'}\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:33.295455Z",
     "start_time": "2025-07-25T17:11:30.794257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "response = await chat_with_tools(\"现在什么时间\")\n",
    "print(response)"
   ],
   "id": "6b7175edf6ac39cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
      "\u001B[32;1m\u001B[1;3mAction:\n",
      "```\n",
      "{\n",
      "  \"action\": \"get_current_date\",\n",
      "  \"action_input\": {}\n",
      "}\n",
      "```\u001B[0m\n",
      "Observation: \u001B[36;1m\u001B[1;3m2025年07月26日 Saturday\u001B[0m\n",
      "Thought:\u001B[32;1m\u001B[1;3mThought: I know what to respond\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"当前日期是2025年07月26日，星期六。\"\n",
      "}\n",
      "```\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "{'input': '现在什么时间', 'output': '当前日期是2025年07月26日，星期六。'}\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-25T17:11:33.386057Z",
     "start_time": "2025-07-25T17:11:33.381385Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "cadfe432f11e0c30",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
